package org.anti996.bankms.controller;

import org.anti996.bankms.dao.UserDao;
import org.anti996.bankms.model.Card;
import org.anti996.bankms.util.IOUtil;
import org.anti996.bankms.util.MessageUtil;
import org.anti996.bankms.view.UserCardChooseView;

import java.util.List;

public class UserPutMoneyInController extends ControllerBase {
    private final List<Card> cards_;
    private final UserCardChooseView userCardChooseView_;

    public UserPutMoneyInController(List<Card> cards) {
        super(null);
        cards_ = cards;
        userCardChooseView_ = new UserCardChooseView(cards);
    }

    @Override
    public void deal() {
        super.deal();
        MessageUtil.title("存款");
        MessageUtil.infoln("选择存入卡");
        userCardChooseView_.show();
        MessageUtil.info("请输入您的选择：");
        int choose = IOUtil.parseOrder();
        if (choose < 1 || cards_.size() < choose) {
            wrongInputAndRetry();
            return;
        }
        Card card_to = cards_.get(choose - 1);
        MessageUtil.info("输入存入金额：");
        double money = Double.parseDouble(IOUtil.parseInput());  // TODO 错误处理
        if (money <= 0) {
            MessageUtil.error("转账金额不能小于0");
        } else {
            boolean result = UserDao.putMoneyIn(card_to, money);
            if (result) {
                MessageUtil.success("存款成功");
                card_to.setBalance(card_to.getBalance() + money);
            } else {
                MessageUtil.error("[内部错误]存款失败");
            }
        }
        Navigator.pop();
    }
}
